package com.shujia.bigdata.word;

import java.io.*;

public class SplitFile {

    public static void main(String[] args) throws Exception {


        String path = "java/data/big_words.txt";

        //获取文件大小
        Long size = getSize(path);

        //获取文件行数
        Long length = getLength(path);


        long lineSize = size / length;


        //每个文件100M

        //每个文件的行数据
        Long fileLength = 50 * 1024 * 1024 / lineSize;


        //计算切分之后文件的数量

        //ceil  向上取整
        Double fileCount = Math.ceil(length / (double) fileLength);


        System.out.println("切分之后文件的个数：" + fileCount);


        FileReader fileReader = new FileReader(path);
        BufferedReader bufferedReader = new BufferedReader(fileReader);


        int inv = 0;

        String outHome = "java/data/word/";

        String outPath = outHome + inv;

        //定义第一个文件输出流对象
        FileWriter fileWriter = new FileWriter(outPath);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);


        String line;

        long flag = 0L;

        while ((line = bufferedReader.readLine()) != null) {

            flag++;
            if (flag == fileLength) {//切换文件输出流对象

                //关闭前一个文件的输出流
                bufferedWriter.close();

                flag = 0L;
                inv++;
                outPath = outHome + inv;
                fileWriter = new FileWriter(outPath);
                bufferedWriter = new BufferedWriter(fileWriter);

            }

            bufferedWriter.write(line);
            bufferedWriter.newLine();

        }

        //关闭最后一个文件
        bufferedWriter.close();
    }


    /**
     * 统计文件的行数
     */

    public static Long getLength(String path) {
        Long count = 0L;

        try {
            FileReader fileReader = new FileReader(path);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String line;

            while ((line = bufferedReader.readLine()) != null) {
                count++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return count;
    }

    /**
     * 获取文件的大小
     */


    public static Long getSize(String path) {

        File file = new File(path);

        long size = file.length();

        return size;
    }

}
